package luanluan.control.managers 
{
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	/**
	 * 舞台管理类
	 * @author luanluan
	 */
	public class StageManager  
	{
		//	*需要控制的舞台对象
		private var _stage:Stage;
		
		public function get stage():Stage 					{			return _stage;			}
		
		public function set stage(value:Stage):void 		{			_stage = value;			}
		
		//*********************************************************//
		//	*是否全屏
		private var _fullScreen:Boolean = false;
		
		public function get fullScreen():Boolean 			{			return _fullScreen;		}
		
		public function set fullScreen(value:Boolean):void 
		{
			if (_fullScreen == value )
				return;
				
			_fullScreen = value;
			
			if (_fullScreen)
				_stage.displayState = StageDisplayState.FULL_SCREEN;
			else
				_stage.displayState = StageDisplayState.NORMAL;
		}
		
		//*********************************************************//
		//	*舞台resize处理器集合
		private var _resizeHandles:Vector.<Function> = new Vector.<Function>();
		//	*处理器数量
		private var _numHandlers:int = 0;
		//	*参数集合
		private var _paramsDic:Dictionary = new Dictionary();
		
		/**
		 * 添加舞台resize处理
		 * @param	handler	舞台触发resize事件的时候执行
		 * @param 	params	相关参数
		 */
		public function addResizeHandle(handler:Function , params:Array = null):void
		{
			if (!_stage.hasEventListener(Event.RESIZE))
			{
				_stage.addEventListener(Event.RESIZE , resizeHandle)
			}
			
			if (_resizeHandles.indexOf(handler) == -1 && handler != null)
			{
				_resizeHandles.push(handler);
				_paramsDic[handler] = params;
				_numHandlers ++;
			}
		}
		
		/**
		 * 移除舞台resize处理
		 * @param	handler	待移除处理器
		 */
		public function removeResizeHandle(handler:Function):void
		{
			if (_resizeHandles.indexOf(handler) != -1)
			{
				_resizeHandles.splice(_resizeHandles.indexOf(handler), 1);
				delete _paramsDic[handler];
				_numHandlers --;
			}
			
			if (_numHandlers == 0)
			{
				_stage.removeEventListener(Event.RESIZE , resizeHandle);
			}
			
		}
		
		private function resizeHandle(e:Event):void 
		{
			for each (var item:Function in _resizeHandles ) 
			{
				item.apply(null, _paramsDic[item]);
			}
		}
		
		//*********************************************************//
		private static var _instance:StageManager;
		
		public function StageManager(single:Single)
		{ 
			if ( !single )
				throw(new Error( "请使用静态getter获取单例引用" ) );
		} 
		
		public static function get instance():StageManager 
		{
			if(_instance == null)
			{
				_instance = new StageManager(new Single());
			}
			return _instance;
		}
		
	}
}
class Single{}